import { notification } from 'antd';
import {
  getRoutesPage,
  getRouteById,
  addRoute,
  delRoute,
  updRoute,
  applyRoute,
} from "../services/route";

export default {
  namespace: 'route',

  state: {
    routes: {
      list: [],
      pagination: {},
    },
    selectedRoute: {},
    modal: {
      width: (window.innerWidth / 2),
      visible: false,
      title: '',
      mode: 'None',
    },
    searchParams: {},
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(getRoutesPage, payload);
      if (response) {
        const { records, total, size, current } = response;
        const routes = {
          list: records,
          pagination: {
            total,
            pageSize: size,
            current,
          },
        };
        yield put({
          type: 'save',
          payload: {
            routes,
          },
        });
        yield put({
          type: 'saveSearchParams',
          payload,
        });
      }
    },
    *operation({ payload }, { call, put }) {
      const { mode, id } = payload;
      if (mode === 'Add') {
        yield put({
          type: 'changeModalStatus',
          payload: {
            selectedRoute: {},
            modal: {
              visible: true,
              title: '新增路由',
              mode,
            },
          },
        });
      } else if (mode === 'Update') {
        const response = yield call(getRouteById, { id });
        if (response) {
          yield put({
            type: 'changeModalStatus',
            payload: {
              selectedRoute: response,
              modal: {
                visible: true,
                title: '修改路由',
                mode,
              },
            },
          });
        } else {
          yield put({
            type: 'initModal',
          });
          notification.error({
            message: '数据错误',
            description: '你选择的数据可能已经不存在，请刷新列表再试！',
          });
        }
      } else if (mode === 'Delete') {
        const response = yield call(getRouteById, { id });
        if (response) {
          yield put({
            type: 'del',
            payload: {
              id,
            },
          });
        } else {
          yield put({
            type: 'initModal',
          });
          notification.error({
            message: '数据错误',
            description: '你选择的数据可能已经不存在，请刷新列表再试！',
          });
        }
      } else if (mode === 'Apply') {
        yield put({
          type: 'apply',
        });
      } else {
        yield put({
          type: 'initModal',
        });
      }
    },
    *add({ payload }, { call, put, select }) {
      const response = yield call(addRoute, payload);
      if (response && response.msg === 'success') {
        const searchParams = yield select(state => state.route.searchParams);
        yield put({
          type: 'initModal',
        });
        yield put({
          type: 'fetch',
          payload: searchParams,
        });
      } else {
        notification.error({
          message: '新增路由失败',
        });
      }
    },
    *del({ payload }, { call, put, select }) {
      const response = yield call(delRoute, payload);
      if (response && response.msg === 'success') {
        const searchParams = yield select(state => state.route.searchParams);
        yield put({
          type: 'fetch',
          payload: searchParams,
        });
      } else {
        notification.error({
          message: '删除数据失败',
          description: '你选择的数据可能已经不存在，请刷新列表再试！',
        });
      }
    },
    *upd({ payload }, { call, put, select }) {
      const response = yield call(updRoute, payload);
      if (response && response.msg === 'success') {
        const searchParams = yield select(state => state.route.searchParams);
        yield put({
          type: 'initModal',
        });
        yield put({
          type: 'fetch',
          payload: searchParams,
        });
      } else {
        notification.error({
          message: '修改路由失败',
          description: '你选择的数据可能已经不存在，请刷新列表再试！',
        });
      }
    },
    *apply(_, { call }) {
      const response = yield call(applyRoute);
      if (response) {
        notification.success({
          message: '同步配置成功',
        });
      } else {
        notification.error({
          message: '同步配置失败',
        });
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    saveSearchParams(state, { payload }) {
      return {
        ...state,
        searchParams: {
          ...payload,
        },
      };
    },
    changeSelectedRoute(state, { payload }) {
      return {
        ...state,
        selectedRoute: {
          ...state.selectedRoute,
          ...payload,
        },
      };
    },
    clearSelectedRoute(state) {
      return {
        ...state,
        selectedRoute: {},
      };
    },
    initModal(state) {
      return {
        ...state,
        selectedRoute: {},
        modal: {
          width: (window.innerWidth / 2),
          visible: false,
          title: '',
          mode: 'None',
        },
      };
    },
    changeModalStatus(state, { payload }) {
      return {
        ...state,
        selectedRoute: {
          ...payload.selectedRoute,
        },
        modal: {
          ...state.modal,
          ...payload.modal,
        },
      };
    },
  },
};
